package no.hig.ekm.ircclient;

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ResourceBundle;
import java.util.prefs.Preferences;

import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.WindowConstants;

/**
 * Generates the main window with all of the components
 * 
 * @author Kristofers, eigil
 */
@SuppressWarnings("serial")
public final class MainWindow extends JFrame {

	public static MainDesktop md = new MainDesktop();
	public static MainChannelTree tree = new MainChannelTree();
	Preferences prefs;
	// All constant variables related to preferances
	private static final String MWXPOS = "mwXPos";
	private static final int MWXPOS_DEF = 150;
	private static final String MWYPOS = "mwYPos";
	private static final int MWYPOS_DEF = 150;
	private static final String MWWIDTH = "mwWidth";
	private static final int MWWIDTH_DEF = 640;
	private static final String MWHEIGHT = "mwHeight";
	private static final int MWHEIGHT_DEF = 480;
	private static final String MWSTATE = "mwState";
	private static final int MWSTATE_DEF = JFrame.NORMAL;
	
	public static boolean closing = false;
	
	/**
	 * Constructor which generates the MainWindow form
	 */
	public MainWindow() {
		// Define main window properties
		super("IRC Client");
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setMinimumSize(new java.awt.Dimension(640, 480));
		setLocationByPlatform(true);

		// Gets a hashmap with program preferences
		prefs = Preferences.userNodeForPackage(getClass());
		// Sets size and position for the window

		setSize(prefs.getInt(MWWIDTH, MWWIDTH_DEF),	prefs.getInt(MWHEIGHT, MWHEIGHT_DEF));
		setLocation(prefs.getInt(MWXPOS, MWXPOS_DEF),prefs.getInt(MWYPOS, MWYPOS_DEF));
		setExtendedState(prefs.getInt(MWSTATE, MWSTATE_DEF));

		// Add the components
		setJMenuBar(new MainMenu());
		add(new MainToolbar(), BorderLayout.NORTH);
		add(tree.getTree(), BorderLayout.WEST);
		add(md, BorderLayout.CENTER);
		addWindowListener(new Closing());
		setVisible(true);
	}

	/**
	 * Method, which adds a fram to the DesktopPane.
	 * 
	 * @param intf - JInternalFrame to be added
	 */
	public void addMDIWin(JInternalFrame intf) {
		md.add(intf);
		// If the ConnectionDialog is up and the new ChannelWindow has appeared
		// set the cursor back to normal. Connection has been finished.
		if ((MainToolbar.cd != null) && MainToolbar.cd.isVisible())
			MainToolbar.cd.setCursor(Cursor
					.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
	}

	class Closing extends WindowAdapter {
		@Override
		public void windowClosing(WindowEvent e) {
			// Save preferences before closing the window
			closing = true;
			Dimension d = getSize();
			Point p = getLocation();
			// Don't save the dimensions and position if the window is maximized
			// so that it can be restored to previous size and position
			if (getExtendedState() != JFrame.MAXIMIZED_BOTH) {
				prefs.putInt(MWWIDTH, d.width);
				prefs.putInt(MWHEIGHT, d.height);
				prefs.putInt(MWXPOS, p.x);
				prefs.putInt(MWYPOS, p.y);
			}
			prefs.putInt(MWSTATE, getExtendedState());
			
			if ((MainToolbar.cd != null)&&(!MainToolbar.cd.isClosed()))
				MainToolbar.cd.doDefaultCloseAction();
		}
	}
}
